package xyf.p02.stack_queue;

import java.util.Stack;

/**
 * ClassName: P0020_ValidBrackets
 * Description:
 * Author: xyf
 * Date: 9/3/22 6:28 PM
 * Version: 1.0
 **/
public class P0020_ValidBrackets
{
    public boolean isValid(String s)
    {
        Stack<Character> stack = new Stack();
        for (int i = 0; i < s.length(); i++)
        {
            if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{')
            {
                stack.push(s.charAt(i));
            }
            else if (s.charAt(i) == ')')
            {
                if (stack.isEmpty())
                {
                    return false;
                }
                Character tmp = stack.pop();
                if (tmp != '(')
                {
                    return false;
                }
            }
            else if (s.charAt(i) == ']')
            {
                if (stack.isEmpty())
                {
                    return false;
                }
                Character tmp = stack.pop();
                if (tmp != '[')
                {
                    return false;
                }
            }
            else if (s.charAt(i) == '}')
            {
                if (stack.isEmpty())
                {
                    return false;
                }
                Character tmp = stack.pop();
                if (tmp != '{')
                {
                    return false;
                }
            }

        }
        return stack.isEmpty();
    }
}
